<?php

namespace Home\Controller;
use Think\Controller;
class PartnerController extends Controller {
    public function aa()
    {
        $password = 123465;
        $a = md5($password);
        var_dump($a);
//        header('Content-type: image/png');
//        $value="http://www.useryx.com";
//        ob_clean();
//        qrcode($value);

    }
    /*************分销商的页面*************/
    public function index()
    {
        $uid = I('get.uid');
        //$uid = 2;
        //当前用户的信息
        $data['user_info'] = query_user($uid);
        //佣金明细
        $data['detail_cash'] = D('Partner_detail_money')->where('online_user_id='.$uid)->select();

        //我的推荐人
        if($data['user_info']['parent_id'] != 0){
            $data['recommend'] = D('Users')->where('id='.$data['user_info']['parent_id'])->getField('nickname');
        }else{
            $data['recommend'] = '平台';
        }

        //我的下线
        $offline_user = query_offline(2);
        //var_dump($offline_user);die;
        $data['num'] = $offline_user['num'];
        $arr= array();
        $offline_user = $offline_user['users'];
        foreach($offline_user as $k=>$v){
            $arr[$k]['user_name'] = $v['nickname'];
            $select = D('Partner_detail_money')->where('online_user_id='.$uid.' And user_id='.$v['id'])->select();
            $cash = 0;
            if(!empty($select)){
                foreach ($select as $key=>$value) {
                    $cash+=$value['cash'];
                }
            }
            $arr[$k]['cash'] = $cash;
        }
        $data['offline_user'] = $arr;

        kuayu();
        echo json_encode($data);
    }

    public function bb()
    {
        $one_rate = 0.09;
        $two_rate = 0.06;
        $three_rate = 0.03;
        $one_partner_rate = 0.07;
        $two_partner_rate = 0.12;
        $three_partner_rate = 0.17;
        $cash = 100.4;
        $user_id = 4;
        $order_id = 6;
        $users = D('Users');
        $partners = D('Partners');
        $partner_detail_money = D('Partner_detail_money');

        $users->startTrans();


        $data['user_id'] = $user_id;
        $data['order_id'] = $order_id;
        $data['time'] = time();

        $user = $users->find($user_id);
        $data['user_name'] = $user['nickname'];
        if($user['partner_id'] != 0) {//判断这用户是否属于加盟商
            $partner = $partners->find($user['partner_id']);
            if ($partner['partner_level'] == 1) {
                $partner_rate = $one_partner_rate;
            } elseif ($partner['partner_level'] == 2) {
                $partner_rate = $two_partner_rate;
            } else {
                $partner_rate = $three_partner_rate;
            }
            $id = $partner_detail_money->max('id');
            $data['id'] = $id + 1;
            $data['partner_id'] = $user['partner_id'];
            $data['cash'] = sprintf("%.2f",$cash * $partner_rate);
            $z = $partner_detail_money->add($data);
            $z1 = $partners->where('id=' . $data['partner_id'])->setInc('commission', $data['cash']);
            if($z && $z1){
                $users->commit();
            }else{
                $users->rollback();
            }
        }

        if($user['parent_id'] != 0){//判断该用户的一级上线
           $data['partner_id'] = 0;
           $id = $partner_detail_money->max('id');
           $data['id'] = $id+1;
           $data['online_user_id'] = $user['parent_id'];
           $data['cash'] =sprintf("%.2f",$cash*$one_rate);
           $z2 = $partner_detail_money->add($data);
           $z3 = $users->where('id='.$data['online_user_id'])->setInc('commission',$data['cash']);

            if($z2 && $z3){
                $users->commit();
            }else{
                $users->rollback();
            }

           $one_user = query_user($user['parent_id']);
           if($one_user['parent_id'] != 0){//判断该用户的二级上线
               $id = $partner_detail_money->max('id');
               $data['id'] = $id+1;
               $data['online_user_id'] = $one_user['parent_id'];
               $data['cash'] =sprintf("%.2f",$cash*$two_rate);
               $z4 = $partner_detail_money->add($data);
               $z5 = $users->where('id='.$data['online_user_id'])->setInc('commission',$data['cash']);

               if($z4 && $z5){
                   $users->commit();
               }else{
                   $users->rollback();
               }

               $two_user = query_user($one_user['parent_id']);
               if($two_user['parent_id'] != 0){//判断该用户的三级上线
                   $id = $partner_detail_money->max('id');
                   $data['id'] = $id+1;
                   $data['online_user_id'] = $two_user['parent_id'];
                   $data['cash'] =sprintf("%.2f",$cash*$three_rate);
                   $z6 = $partner_detail_money->add($data);
                   $z7 = $users->where('id='.$data['online_user_id'])->setInc('commission',$data['cash']);

                   if($z6 && $z7){
                       $users->commit();
                   }else{
                       $users->rollback();
                   }
               }
           }
       }


    }

    //分销中心
    public function Partner_index()
    {

    }
}